<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1>Vue3 Demo</h1>
        <p class="subtitle">欢迎回来，请登录您的账号</p>
      </div>
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-item">
          <label>用户名</label>
          <div class="input-wrapper">
            <span class="icon">👤</span>
            <input v-model="loginForm.username" type="text" required placeholder="请输入用户名">
          </div>
        </div>
        <div class="form-item">
          <label>密码</label>
          <div class="input-wrapper">
            <span class="icon">🔒</span>
            <input v-model="loginForm.password" type="password" required placeholder="请输入密码">
          </div>
        </div>
        <div class="form-actions">
          <button type="submit" class="login-btn">
            <span>登录</span>
          </button>
          <router-link to="/register" class="register-link">
            还没有账号？立即注册
          </router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { userApi } from '@/api/user'
import type { LoginData } from '@/api/user'

const router = useRouter()
const loginForm = ref<LoginData>({
  username: 'admin',
  password: '123456'
})

const handleLogin = async () => {
  try {
    const { data } = await userApi.login(loginForm.value)
    if (data.code === 200) {
      localStorage.setItem('token', data.data.token)
      localStorage.setItem('user', JSON.stringify(data.data.user))
      router.push('/user/' + data.data.user.id)
    } else {
      alert(data.message)
    }
  } catch (error) {
    console.error('登录失败:', error)
    alert('登录失败，请重试')
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 420px;
  padding: 2.5rem;
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.login-header {
  text-align: center;
  margin-bottom: 2.5rem;
}

h1 {
  font-size: 2rem;
  color: #2d3748;
  margin: 0;
  margin-bottom: 0.5rem;
}

.subtitle {
  color: #718096;
  margin: 0;
  font-size: 0.95rem;
}

.login-form {
  margin-top: 2rem;
}

.form-item {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  color: #4a5568;
  font-weight: 500;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.icon {
  position: absolute;
  left: 12px;
  color: #a0aec0;
  font-size: 1.2rem;
}

input {
  width: 100%;
  padding: 0.75rem 1rem 0.75rem 2.5rem;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: #f8fafc;
}

input:focus {
  outline: none;
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

input::placeholder {
  color: #a0aec0;
}

.form-actions {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 2rem;
}

.login-btn {
  width: 100%;
  padding: 0.875rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  transition: all 0.3s ease;
}

.login-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}

.login-btn:active {
  transform: translateY(0);
}

.register-link {
  text-align: center;
  color: #667eea;
  text-decoration: none;
  font-size: 0.95rem;
  transition: color 0.3s ease;
}

.register-link:hover {
  color: #764ba2;
  text-decoration: underline;
}

@media (max-width: 480px) {
  .login-box {
    padding: 1.5rem;
    margin: 1rem;
    width: calc(100% - 2rem);
  }

  h1 {
    font-size: 1.75rem;
  }

  .subtitle {
    font-size: 0.9rem;
  }

  input {
    font-size: 16px;
    /* 防止iOS自动缩放 */
  }
}

@media (min-width: 481px) and (max-width: 768px) {
  .login-box {
    width: 90%;
    max-width: 400px;
  }
}

@media (min-width: 769px) {
  .login-box {
    width: 420px;
  }
}

/* 添加深色模式支持 */
@media (prefers-color-scheme: dark) {
  .login-box {
    background: #1a1a1a;
  }

  h1 {
    color: #ffffff;
  }

  .subtitle {
    color: #a0aec0;
  }

  input {
    background: #2d3748;
    border-color: #4a5568;
    color: #ffffff;
  }

  input:focus {
    border-color: #667eea;
    background: #2d3748;
  }

  input::placeholder {
    color: #718096;
  }

  label {
    color: #e2e8f0;
  }
}
</style>